package com.xce.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xce.order.feign.AccountFeign;
import com.xce.order.entity.OrderTbl;
import com.xce.order.mapper.OrderTblMapper;
import com.xce.order.service.OrderTblService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lld
 * @since 2025-07-09
 */
@Service
public class OrderTblServiceImpl extends ServiceImpl<OrderTblMapper, OrderTbl> implements OrderTblService {

    @Autowired
    private OrderTblMapper orderTblMapper;

    @Autowired
    private AccountFeign accountFeign;
    @Override
    @Transactional
    public OrderTbl create(String userId, String commodityCode, Integer count) {

        // 1.计算订单金额
        int orderMoney = calculate(commodityCode, count);

        // 2.扣减账户余额
        accountFeign.debit(userId, orderMoney);
        // 3.保存订单
        OrderTbl orderTbl = new OrderTbl();
        orderTbl.setUserId(userId);
        orderTbl.setCommodityCode(commodityCode);
        orderTbl.setCount(count);
        orderTbl.setMoney(orderMoney);
        orderTblMapper.insert(orderTbl);

        int i = 10/0;
        return orderTbl;
    }

    private int calculate(String commodityCode, Integer count) {
        return 9*count;
    }
}
